package demo2;

import java.io.*;
import java.util.Scanner;

public class LoginTest {
    public static void main(String[] args) throws IOException {
        //本地文件实现登陆了功能,超过三次站好锁定
        //1.读取本地正确账密
        BufferedReader br = new BufferedReader(new FileReader("IOTest\\src\\demo2\\user.txt"));
        //1.1获取到详细数据
        String user = br.readLine();
        br.close();
        String username = user.split("&")[0];
        String password = user.split("&")[1];
        String count = user.split("&")[2];
        System.out.println(count);
        String rightusername = username.split("=")[1];
        String rightpassword = password.split("=")[1];
        int freeCount = Integer.parseInt(count.split("=")[1]);
        //2.用户键盘录入
        Scanner sc = new Scanner(System.in);
        User userInfo = new User(rightusername,rightpassword,freeCount);
        //3.进行判断
        while (true) {
            if(freeCount >= 3){
                System.out.println("输入错误超过三次,账号已被锁定");
                userInfo = new User(rightusername,rightpassword,freeCount);
                break;
            }
            System.out.println("请输入账号:");
            String inputname = sc.nextLine();
            System.out.println("请输入密码:");
            String inputpwd = sc.nextLine();
            if (rightusername.equals(inputname) && rightpassword.equals(inputpwd)) {
                freeCount = 0;
                System.out.println("登录成功");
                userInfo = new User(rightusername,rightpassword,freeCount);
                break;
            } else {
                freeCount++;
                System.out.println("账号或密码输入错误,还有" + (3-freeCount) + "次机会,请重新输入:");
            }
        }
        //4.将登录过后的数据进行保存到本地文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter("IOTest\\src\\demo2\\user.txt"));
        bw.write(userInfo.toString());
        bw.close();

    }
}
